﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_Page_Mes : IDisposable
    {
        private SqlDataAdapter dscommand;

        public b_Page_Mes()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }
        //
        //指令资源分配方式的代码段，与逻辑无关
        //

        public b_Page_Mesdata getpagemesbyNO(int mesid)
        {
            //输入一个留言标号，返回对应的留言
            //如果不存在返回的是个空数据
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Page_Mes WHERE Mes_ID=" + mesid .ToString ();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Page_Mes");
            tempconn.Close();
            return (newdata);
        }
        public bool judgemesbyNO(int mesid)
        {
            //输入一个留言标号，判断是否存在对应的留言
            //存在返回true，不存在返回false
            if(getpagemesbyNO(mesid ).Tables ["b_Page_Mes"].Rows .Count  == 0)
                return false;
            else 
                return true;
        }

      

        public b_Page_Mesdata getpagemesbyID(string username)
        {
            //输入一个用户的注册邮箱，返回其页面的留言列表
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Page_Mes WHERE Host_User_ID='" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Page_Mes");
            tempconn.Close();
            return (newdata);
        }

        public int countmesbyname(string username)
        {
            //输入一个用户的注册邮箱，返回其页面的留言总数
            return (getpagemesbyID(username).Tables["b_Page_Mes"].Rows.Count);
        }

        public bool deletemesbyNO(int mesid)
        {
            //根据页面留言编号删除一篇留言
            //如果存在对应留言并成功删除返回true,否则返回false
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Page_Mes WHERE Mes_ID =" + mesid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Page_Mes");

            if (newdata.Tables["b_Page_Mes"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Page_Mes"].Rows[0].Delete();
                dscommand.Update(newdata, "b_Page_Mes");
                tempconn.Close();
                return (true);
            }
        }
        public bool deletemesbyname(string username)
        {
            //根据用户注册邮箱删除其主页面的所有留言
            //如果用户存在并成功删除，则返回true，否则返回false
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Page_Mes WHERE Host_User_ID = '" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Page_Mes");

            if (newdata.Tables["b_Page_Mes"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = newdata.Tables["b_Page_Mes"].Rows.Count; i > 0; i--)
                {
                    newdata.Tables["b_Page_Mes"].Rows[i - 1].Delete();
                }
                dscommand.Update(newdata, "b_Page_Mes");
                tempconn.Close();
                return (true);
            }
        }

        public bool modifypagemes(b_Page_Mesdata tempdata)
        {
            //输入一个页面留言数据，如果存在对应ID则修改，并返回true，否则返回False
            int mesid = Int32.Parse(tempdata.Tables["b_Page_Mes"].Rows[0]["Mes_ID"].ToString());
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Page_Mes WHERE Mes_ID =" + mesid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Page_Mes");
            if (newdata.Tables["b_Page_Mes"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = 0; i < tempdata.Tables["b_Page_Mes"].Columns.Count; i++)
                    newdata.Tables["b_Page_Mes"].Rows[0][i] = tempdata.Tables["b_Page_Mes"].Rows[0][i];
                dscommand.Update(newdata, "b_Page_Mes");
                tempconn.Close();
                return (true);
            }
        }

        public bool insertpagemes(b_Page_Mesdata tempdata)
        {
            //输入一个页面留言数据，插入到数据库中，并且返回true
            string hostuserid = tempdata.Tables["b_Page_Info"].Rows[0][1].ToString();
            string guestid = tempdata.Tables["b_Page_Info"].Rows[0][2].ToString();
            string mescontent = tempdata.Tables["b_Page_Info"].Rows[0][3].ToString();
            string mesdate = tempdata.Tables["b_Page_Info"].Rows[0][4].ToString();
            string insertstring = "INSERT b_Diary_Mes (Diary_ID,Guest_ID,Mes_content,Mes_Date) VALUES('" + hostuserid + ",'" + guestid + "','" + mescontent + "','" + mesdate + "')";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return (true);
        }

    }
}
